package com.becca.cake.service.system.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.dao.system.AdminRoleDAO;
import com.becca.cake.dao.system.RoleFunctionDAO;
import com.becca.cake.dao.system.RolesDAO;
import com.becca.cake.model.system.RoleDO;
import com.becca.cake.service.system.IUserRoleService;
import com.becca.cake.util.JsonUtil;

/**
 * 角色管理Service层实现类
 * @author hejinguo
 * @version $Id: UserRoleServiceImpl.java, v 0.1 2013-12-2 下午11:02:26
 */
@Service
public class UserRoleServiceImpl implements IUserRoleService {
    @Autowired
    private RolesDAO        rolesDAO;
    @Autowired
    private AdminRoleDAO    adminRoleDAO;
    @Autowired
    private RoleFunctionDAO roleFunctionDAO;

    @Override
    @Cacheable(key = "'selectAllRoles'", value = "monmondecakeCache")
    public List<RoleDO> selectAllRoles() throws Exception {
        return this.rolesDAO.selectAllRoles();
    }

    @Override
    public List<RoleDO> selectAllRoleMessage(Map<String, Object> map) throws Exception {
        return this.rolesDAO.selectAllRoleMessage(map);
    }

    @Override
    public Long selectAllRoleMessageCount(Map<String, Object> map) throws Exception {
        return this.rolesDAO.selectAllRoleMessageCount(map);
    }

    @Override
    public String selectRoleById(String roleId) throws Exception {
        RoleDO roleDO = this.rolesDAO.selectRoleById(roleId);
        return JsonUtil.writeObjectFormJSON(roleDO);
    }

    @Override
    @CacheEvict(key = "'selectAllRoles'", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insetOrUpdateRole(RoleDO roleDO) throws Exception {
        if (roleDO.getId() != null) {
            this.rolesDAO.updateRole(roleDO);
        } else {
            this.rolesDAO.insetRole(roleDO);
        }
    }

    @Override
    @CacheEvict(key = "'selectAllRoles'", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void deleteRoleById(String ids) throws Exception {
        //删除员工角色表信息
        this.adminRoleDAO.deleteAdminRoleById(ids);
        //删除功能角色信息
        this.roleFunctionDAO.deleteRoleFunctionById(ids);
        //删除角色信息
        this.rolesDAO.deleteRoleById(ids);
    }

    @Override
    public String selectRoleByAdminId(String adminId) throws Exception {
        List<RoleDO> listRole = this.rolesDAO.selectRoleByAdminId(adminId);
        return JsonUtil.writeObjectNoPageJSON(listRole);
    }

    @Override
    public String selectRoleByDeptId(Map<String, Object> map) throws Exception {
        List<RoleDO> listRole = this.rolesDAO.selectRoleByDeptId(map);
        Long count = this.rolesDAO.selectRoleByDeptIdCount(map);
        return JsonUtil.writeObjectPageJSON(listRole, count);
    }

    @Override
    @CacheEvict(key = "'selectAllRoles'", value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertAdminRole(Map<String, Object> map) throws Exception {
        //根据员工ID删除员工角色信息
        this.adminRoleDAO.deleteAdminRoleByAdminId(map.get("adminId").toString());
        //添加员工角色信息
        this.adminRoleDAO.insertAdminRole(map);
    }

}
